/*
 * DxfLTYPE.java
 *
 * Created on 5 de Dezembro de 2007, 15:01
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package importadorDXF;

import java.io.PrintStream;
import java.util.Enumeration;
import java.util.Vector;

/**
 *
 * @author rafaelbuck
 */
public class DxfLTYPE extends DxfTable
{

    /** Creates a new instance of DxfLTYPE */
    DxfLTYPE()
    {
        this("CONTINUOUS");
    }

    /** Creates a new instance of DxfLTYPE */
    DxfLTYPE(String name)
    {
        isConti = false;
        super.name = name.toUpperCase();
    }

    public boolean setGroup(short grpNr, String str)
    {
        if(grpNr == 3)
        {
            description = str;
            return true;
        } else
        {
            return super.setGroup(grpNr, str);
        }
    }

    public boolean setGroup(short grpNr, int ival)
    {
        switch(grpNr)
        {
        case 72: // 'H'
            adjust = (short)ival;
            break;

        case 73: // 'I'
            if(pattern == null && ival > 0)
                pattern = new Vector(ival);
            break;

        default:
            return super.setGroup(grpNr, ival);
        }
        return true;
    }

    public boolean setGroup(short grpNr, float fval)
    {
        switch(grpNr)
        {
        case 40: // '('
            length = fval;
            break;

        case 49: // '1'
            if(pattern == null)
                pattern = new Vector(5);
            pattern.addElement(new Float(fval));
            break;

        default:
            return super.setGroup(grpNr, fval);
        }
        return true;
    }

    public boolean finishRead(DxfFile file)
    {
        float segments[] = getPattern();
        float reallength = 0.0F;
        if(segments != null)
        {
            for(int s = segments.length - 1; s >= 0; s--)
                reallength += Math.abs(segments[s]);

        }
        if(reallength != length)
        {
            Math.abs(length - reallength);
            length = reallength;
        }
        isConti = "CONTINUOUS".equals(super.name) || length == 0.0F;
        return true;
    }

    public boolean isContinuous()
    {
        return isConti;
    }

    public float getPatternLength()
    {
        return length;
    }

    public float[] getPattern()
    {
        if(isConti || pattern == null)
            return null;
        float ret[] = new float[pattern.size()];
        int pos = 0;
        for(Enumeration e = pattern.elements(); e.hasMoreElements();)
            ret[pos++] = ((Float)e.nextElement()).floatValue();

        return ret;
    }

    public String getDescription()
    {
        return description;
    }

    public short getAdjustmentFlags()
    {
        return adjust;
    }

    public static final short LTYPE_XREF = 16;
    public static final short LTYPE_XREF_FOUND = 32;
    public static final short LTYPE_USED = 64;
    public static final String CONTINUOUS = "CONTINUOUS";
    public static final String BYBLOCK = "BYBLOCK";
    public static final String BYLAYER = "BYLAYER";
    private boolean isConti;
    private String description;
    private short adjust;
    private float length;
    private Vector pattern;
}